﻿using System;
using System.Data.Common;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace FlexField.Metadata.EFCore.Tests
{
    public class ScratchDatabaseFixture : IDisposable
    {
        private readonly SqliteConnection _connection;

        public ScratchDatabaseFixture()
        {
            _connection = new SqliteConnection($"DataSource=D{Guid.NewGuid()}.db");

            using var context = CreateEmptyContext();
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
        }

        private DbContext CreateEmptyContext() => new(new DbContextOptionsBuilder().UseSqlite(_connection).Options);

        public DbConnection Connection => _connection;

        public void Dispose()
        {
            using (var context = CreateEmptyContext())
            {
                context.Database.EnsureDeleted();
            }

            _connection.Dispose();
        }
    }
}
